package com.auotc.demo

import android.app.Fragment
import android.app.FragmentManager
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import android.support.v4.view.PagerAdapter
import android.support.v7.app.AppCompatActivity
import android.view.View
import kotlinx.android.synthetic.main.activity_two.*

/**
 * Created by lgs on 2019/4/29.
 */
class TwoActivity : AppCompatActivity() {

    lateinit var datas: MutableList<String>
    val fragments: MutableList<Fragment> = mutableListOf()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_two)
        initViewPager()
    }

    private fun initViewPager() {
        datas = mutableListOf(
                "要闻", "娱乐", "科技", "汽车", "体育",
                "推荐", "文化", "电影", "星座", "汽车", "股票")
        for (titel in datas)
            fragments.add(VpFragment())
        viewpager.adapter = MyPagerAdapter(supportFragmentManager)
        tablayout.setupWithViewPager(viewpager)
    }

    inner class MyPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {

        override fun getItem(p0: Int) = fragments[p0]

        override fun getCount() = fragments.size

        override fun getPageTitle(position: Int): CharSequence? {
            return datas[position]
        }


    }
}